package io.renren.modules.gift.service.impl;

import io.renren.common.config.FileServerConfig;
import io.renren.common.exception.RRException;
import io.renren.common.utils.DfsUtil;
import io.renren.common.utils.FileServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import io.renren.modules.gift.dao.PointsExchangeDao;
import io.renren.modules.gift.entity.PointsExchangeEntity;
import io.renren.modules.gift.service.PointsExchangeService;



@Service("pointsExchangeService")
public class PointsExchangeServiceImpl implements PointsExchangeService {
	@Autowired
	private PointsExchangeDao pointsExchangeDao;

    @Autowired
	private FileServerConfig fileServerConfig;
	
	@Override
	public PointsExchangeEntity queryObject(String id){
		return pointsExchangeDao.queryObject(id);
	}
	
	@Override
	public List<PointsExchangeEntity> queryList(Map<String, Object> map){
		return pointsExchangeDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return pointsExchangeDao.queryTotal(map);
	}
	
	@Override
	public void save(PointsExchangeEntity pointsExchange){
        uploadImage(pointsExchange);
        pointsExchangeDao.save(pointsExchange);
	}
	
	@Override
	public void update(PointsExchangeEntity pointsExchange){
        uploadImage(pointsExchange);
	    pointsExchangeDao.update(pointsExchange);
	}
	
	@Override
	public void delete(String id){
		pointsExchangeDao.delete(id);
	}
	
	@Override
	public void deleteBatch(String[] ids){
		pointsExchangeDao.deleteBatch(ids);
	}

	private void uploadImage(PointsExchangeEntity pointsExchange){
        if (!pointsExchange.getImagesrc().isEmpty()) {
            try {
                FileServerResponse response = DfsUtil.uploadFileByMultipartFile(pointsExchange.getImagesrc(), fileServerConfig.getImageUploadUrl());
                pointsExchange.setUrl(response.getRealHttpUrl());
            } catch (IOException e) {
                throw new RRException(e.getMessage());
            }
        }
    }
	
}
